package _03_链表;

import _01_第一季._00_公共接口.AbstractList;

import java.util.Objects;

/*
    单向循环链表
    添加和删除是考虑头节点添加的情况，中间和尾节点可以统一操作
 */
public class SingleCircleList<E> extends AbstractList<E> {
    private Node<E> first;

    private static class Node<E>{
        E element;
        Node<E> next;

        public Node() {
            this(null,null);
        }

        public Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();

            sb.append(element).append("_").append(next.element);


            return sb.toString();
        }
    }

    //根据索引找到内部的节点
    private Node<E> node(int index){
        rangeCheck(index);
        Node<E> node = first;
        for(int i=0;i<index;++i){
            node = node.next;
        }
        return node;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        if(index == 0){ //头节点添加
            Node<E> newFirst = new Node<>(element,first);

            Node<E> last = (size == 0) ? newFirst : node(size-1);
            last.next = newFirst;
            first = newFirst;
        }else{
            Node<E> prev = node(index - 1);
            prev.next = new Node<>(element,prev.next);
        }
        size++;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E ret = node.element;
        node.element = element;
        return ret;
    }

    @Override
    public void remove(E element) {
        int index = indexOf(element);
        if(index!=ELEMENT_NOT_FOUND){
            remove(index);
        }else{
            throw new IllegalArgumentException("element not found");
        }
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> node = first;
        if(index == 0){ //删除头节点
            if(size == 1){
                first = null;
            }else{
                Node<E> last = node(size - 1);
                first = first.next;
                last.next = first;
            }
        }else{ //删除中间和尾节点
            Node<E> prev = node(index - 1);
            node = prev.next;
            prev.next = node.next;
        }
        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = first; //从头结点开始遍历
        for(int i=0;i<size;++i){
            if(valEquals(element,node.element)) return i;
            node = node.next;
        }
        return ELEMENT_NOT_FOUND;
    }
    private boolean valEquals(Object v1,Object v2){
        return Objects.equals(v1, v2);
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(String.format("LinkedSinglyList:%d [",size)); //只有元素个数
        if(isEmpty()){
            sb.append(']');
        }else {
            Node<E> p =first; //代替头指针去遍历链表
            while(true){
                sb.append(p.element); //拼接数据
                if(p.next==null){ //如果到达了最后一个结点
                    sb.append(']');
                    break;
                }
                //不是最后一个结点，就拼接,号，并向后移
                sb.append(',');
                p=p.next;
            }
        }
        return sb.toString();
    }
}
